import {
  OrderModel
} from '../../model/order'
import {
  uploadCOSTxCloud
} from '../../utils/cloud'
var order = new OrderModel()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    imgs: [],
    contentText: '',
    content: 0,
    status: '',
    statusId: 0,
    showActionsheet: false,
    groups: [{
        text: '准备中',
        value: "1"
      },
      {
        text: '备料中 ',
        value: "2"
      },
      {
        text: '打版中 ',
        value: "3"
      },
      {
        text: '样衣制作 ',
        value: "4"
      },
      {
        text: '完成刺绣 ',
        value: "5"
      },
      {
        text: '成衣制作 ',
        value: "6"
      },
      {
        text: '调整中 ',
        value: "7"
      },
      {
        text: '已完成 ',
        value: "8"
      },
    ],
  },
  close: function () {
    this.setData({
      showActionsheet: false,
    })
  },
  btnClick(e) {
    console.log(e)
    this.setData({
      status: this.data.groups[e.detail.index].text,
      statusId: e.detail.value
    })
    this.close()
  },
  chooseStatus() {
    this.setData({
      showActionsheet: true
    })
  },
  inputs: function (e) {
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);
    this.setData({
      contentText: value,
      content: len
    })
  },
  submit() {
    var images = this.data.imgs.join(";;")
    if (this.data.statusId === 0) {
      wx.showToast({
        title: '请选择状态',
        icon: 'none'
      })
    }else if (this.data.content === 0) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none'
      })
    } else if (this.data.imgs.length < 3) {
      wx.showToast({
        title: '图片最少上传3张',
        icon: 'none'
      })
    } else {
      order.customMadeDiarySave(this.data.id, this.data.contentText, images, this.data.statusId).then(
        res => {
          if (res.code === 0) {
            wx.showToast({
              title: '发布成功',
            })
            wx.navigateBack({})
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        }
      )
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  chooseImg() {
    let that = this;
    let len = this.data.imgs;
    if (len >= 9) {
      this.setData({
        lenMore: 1
      })


      return;
    }
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.showLoading({
          title: '图片正在上传中',
        })
        console.log(res)
        let tempFilePaths = res.tempFilePaths;
        let imgs = that.data.imgs;
        for (let i = 0; i < tempFilePaths.length; i++) {
          uploadCOSTxCloud('', 'voice', tempFilePaths[i]).then(
            res => {
              if (imgs.length < 9) {
                imgs.push(res)
                that.setData({
                  imgs
                })
              } else {
                that.setData({
                  imgs
                })
                wx.showModal({
                  title: '提示',
                  content: '最多只能有九张图片'
                })
                return;
              }
            }
          )
        }
        that.setData({
          imgs
        })
      }
    })
  },
  previewImg(e) {
    let index = e.currentTarget.dataset.index;
    let imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs,
    })
  },
  deleteImg(e) {
    let _index = e.currentTarget.dataset.index;
    let imgs = this.data.imgs;
    imgs.splice(_index, 1);
    this.setData({
      imgs
    })
  }

})